package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunxinyong.cec.entity.Cec711FourNonprofitOrganization;
import net.yunxinyong.cec.entity.Cec711FourNonprofitOrganizationExample;
import net.yunxinyong.cec.mapper.Cec711FourNonprofitOrganizationMapper;
import net.yunxinyong.cec.service.Cec711FourNonprofitService;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Cec711FourNonprofitServiceImpl implements Cec711FourNonprofitService {
    @Autowired
    private Cec711FourNonprofitOrganizationMapper cec711FourNonprofitOrganizationMapper;
    @Override
    public int insertFour(Cec711FourNonprofitOrganization cec711FourNonprofitOrganization) {
        //这里先简单的根据base_id进行去重判断,后续有需求可以更改.
        Cec711FourNonprofitOrganizationExample cec711FourNonprofitOrganizationExample = new Cec711FourNonprofitOrganizationExample();
        cec711FourNonprofitOrganizationExample.createCriteria().andBaseIdEqualTo(cec711FourNonprofitOrganization.getBaseId());
        List<Cec711FourNonprofitOrganization> cec711FourNonprofitOrganizationList = cec711FourNonprofitOrganizationMapper.selectByExample(cec711FourNonprofitOrganizationExample);
        if(!cec711FourNonprofitOrganizationList.isEmpty()){
            return 100;
        }
        cec711FourNonprofitOrganization.setCreateTime(new Date());
        cec711FourNonprofitOrganization.setUpdateTime(new Date());
        int i = cec711FourNonprofitOrganizationMapper.insertSelective(cec711FourNonprofitOrganization);
        return i;
    }

    @Override
    public int deleteFour(Integer id) {
        //根据base_id进行删除
        Cec711FourNonprofitOrganization cec711FourNonprofitOrganization = new Cec711FourNonprofitOrganization();
        cec711FourNonprofitOrganization.setBaseId(id);
        cec711FourNonprofitOrganization.setStatus(0);

        int i = cec711FourNonprofitOrganizationMapper.updateByPrimaryKeySelective(cec711FourNonprofitOrganization);
        return i;
    }

    @Override
    public int update(Cec711FourNonprofitOrganization cec711FourNonprofitOrganization) {
        cec711FourNonprofitOrganization.setUpdateTime(new Date());
        int i = cec711FourNonprofitOrganizationMapper.updateByPrimaryKeySelective(cec711FourNonprofitOrganization);
        return i;
    }

    @Override
    public PageBean ListFourByName(int page ,int rows,Cec711FourNonprofitOrganization cec711FourNonprofitOrganization) {
        //这里没有加任何的查询条件
        if(page !=0 && rows!=0){
            PageHelper.startPage(page,rows);
        }
        Cec711FourNonprofitOrganizationExample cec711FourNonprofitOrganizationExample = new Cec711FourNonprofitOrganizationExample();
        List<Cec711FourNonprofitOrganization> organizationList = cec711FourNonprofitOrganizationMapper.selectByExample(cec711FourNonprofitOrganizationExample);
        PageBean<Cec711FourNonprofitOrganization> pageBean = new PageBean<>();
        PageInfo<Cec711FourNonprofitOrganization> pageInfo = new PageInfo<>(organizationList);
        pageBean.setPageDatas(organizationList);
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setTotal(pageInfo.getTotal());
        return pageBean;

    }
}
